// TF_CAXView.cpp : implementation of the CTF_CAXView class
//

#include "stdafx.h"
#include "TF_CAX.h"

#include "TF_CAXDoc.h"
#include "TF_CAXView.h"
#include "../TF_Geom/tf_Geom_Algorithm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTF_CAXView

#include "ArcBall.h"

ArcBallT arcBall(600.0f,400.0f);
ArcBallT*    ArcBall =&arcBall;


IMPLEMENT_DYNCREATE(CTF_CAXView, CView)

BEGIN_MESSAGE_MAP(CTF_CAXView, CView)
	//{{AFX_MSG_MAP(CTF_CAXView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTF_CAXView construction/destruction

CTF_CAXView::CTF_CAXView()
{
	// TODO: add construction code here
	m_pGLDC = NULL;
}

CTF_CAXView::~CTF_CAXView()
{
}

BOOL CTF_CAXView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.style |= WS_CLIPSIBLINGS|WS_CLIPCHILDREN;
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTF_CAXView drawing

void CTF_CAXView::OnDraw(CDC* pDC)
{
// 	CTF_CAXDoc* pDoc = GetDocument();
// 	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here


	if(m_pGLDC)
	{
		m_pGLDC->Ready();
		RenderScene(m_pGLDC);
		m_pGLDC->Finish();
	}
}

/////////////////////////////////////////////////////////////////////////////
// CTF_CAXView printing

BOOL CTF_CAXView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTF_CAXView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTF_CAXView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTF_CAXView diagnostics

#ifdef _DEBUG
void CTF_CAXView::AssertValid() const
{
	CView::AssertValid();
}

void CTF_CAXView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTF_CAXDoc* CTF_CAXView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTF_CAXDoc)));
	return (CTF_CAXDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTF_CAXView message handlers



void CTF_CAXView::RenderScene(TfOpenGLDC* pDC)
{
	if (ArcBall->zoomRate < 0.000001)
	{
		ArcBall->zoomRate = 0.000001;
	}
	glScalef(ArcBall->zoomRate, ArcBall->zoomRate, ArcBall->zoomRate);//Scale
    glMultMatrixf(ArcBall->Transform.M);                        //Rotate
	pDC->DrawCoord();
	pDC->DrawLine();
	pDC->DrawElement();
}
void CTF_CAXView::OnViewType(UINT type)
{
	ASSERT(type >= VIEW_FRONT && type <= VIEW_NW_ISOMETRIC);
	m_pGLDC->m_Camera.SetViewType(type);
	Invalidate();
}

BOOL CTF_CAXView::GetBox(double& x0,double& y0,double& z0,double& x1,double& y1,double& z1)
{
	return FALSE;
}

void CTF_CAXView::ZoomAll()
{
	double x0 = -62,y0 = -50.0 ,z0 = -42.0,x1 = 45,y1 = 50,z1 = 0;
	GetModelsBox(x0,y0,z0,x1,y1,z1);
//	if(GetBox(x0,y0,z0,x1,y1,z1))
	{
		m_pGLDC->m_Camera.ZoomAll(x0,y0,z0,x1,y1,z1);
		Invalidate();
	}
}

void CTF_CAXView::Zoom(double dScale)
{
	m_pGLDC->m_Camera.Zoom(dScale);
	Invalidate();
}

void CTF_CAXView::MoveView(double dpx,double dpy)
{
//	m_pGLDC->m_Camera.MoveView(dpx,dpy);
	Invalidate();
}


int CTF_CAXView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here

	m_pGLDC = new TfOpenGLDC(this->GetSafeHwnd());
	m_pGLDC->InitDC();

	return 0;
}

void CTF_CAXView::OnDestroy() 
{
	if(m_pGLDC)	delete m_pGLDC;	/*have some problem*/

	CView::OnDestroy();
	
	// TODO: Add your message handler code here
}

void CTF_CAXView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(m_pGLDC)	
		m_pGLDC->GLResize(cx,cy);

		double nRange = 2400.0;
		ArcBall->setBounds((GLfloat)nRange, (GLfloat)nRange);

}

void CTF_CAXView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	// TODO: Add your message handler code here and/or call default
	ArcBall->isClicked = true;
	ArcBall->MousePt.s.X = point.x;
	ArcBall->MousePt.s.Y = point.y;
	ArcBall->upstate();
	Invalidate(FALSE);

	CView::OnLButtonDown(nFlags, point);
}

void CTF_CAXView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (ArcBall->isClicked == true || ArcBall->isRClicked == true)
	{
		ArcBall->MousePt.s.X = point.x;
		ArcBall->MousePt.s.Y = point.y;
		ArcBall->upstate();
		Invalidate(FALSE);
	}

	CView::OnMouseMove(nFlags, point);
}

void CTF_CAXView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	ArcBall->isClicked = false;
	ArcBall->upstate();
	Invalidate(FALSE);

	CView::OnLButtonUp(nFlags, point);
}

void CTF_CAXView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	ArcBall->isRClicked = true;
	ArcBall->MousePt.s.X = point.x;
	ArcBall->MousePt.s.Y = point.y;
	ArcBall->upstate();
	Invalidate(FALSE);

	CView::OnRButtonDown(nFlags, point);
}

void CTF_CAXView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	ArcBall->isRClicked = false;
	ArcBall->upstate();
	Invalidate(FALSE);

	CView::OnRButtonUp(nFlags, point);
}
